/*	Level sends input to the Player class. (If you're in the Shop, the Player class receives no input.)
 * 
 * 
 */

import java.awt.event.*;
import java.awt.*;

public class Player extends Ship {
	
	// for movement keys being down
	private boolean up, down;
	private boolean left, right;
	
	// for weapon keys being down
	private boolean missile; // can simply hold down to fire at maximum rate
	private boolean bomb; // must release & press again to activate again, imo (or have a high frequencyCap[longer])
	
	public Player(){
		setImageFileName("First Ship.gif");
		setX(380);
		setY(285);
		setWidth(56);
		setDepth(57);
		setTraversalSpeed(300);
		setPath(new Path(getX(),getY()));
	}
	public Player(Level cc, String cifn, int cx, int cy, int cw, int cd){
		super(cc,cifn,cx,cy,cw,cd);
		setImageFileName("First Ship.gif");
		setTraversalSpeed(100);
		setPath(new Path(getX(),getY()));
	}
	
	public void update(long l){
		Point temp = new Point();
		temp = getPath().addTravelDistance(l, (int)getTraversalSpeed());
		setX((int)temp.getX());
		setY((int)temp.getY());
		// @ABOVE - I DON'T EVEN KNOW WHAT THE FUCK IS GOING ON, ROFLMAO!
		
		/*
		setGameInterface(getContainer().getGameInterface());
		if(getX()+getWidth()<getGameInterface().getDimensions().getWidth()+getGameInterface().getInsetAdjustment().getX())
			setX(getX()+1);
		if(getY()+getDepth()<getGameInterface().getDimensions().getHeight()+getGameInterface().getInsetAdjustment().getY())
			setY(getY()+1);*/
	}
	public void display(Graphics2D g){
		g.drawImage(getContainer().getGameInterface().getImageLoader().getImage(getImageFileName()),(int)getX(),(int)getY(),getContainer().getGameInterface());
		//System.out.println("getting here as well?");
	}
	
	public boolean mouseInput(MouseEvent me){
		return false;
	}
	public boolean keyInput(KeyEvent ke){
		//System.out.println(ke.getKeyCode());
		boolean tookInput = false;
		if(ke.getID() == 401)
		{
			if(ke.getKeyCode() == 37)
			{
				left = true;
				tookInput = true;
			}
			if(ke.getKeyCode() == 38)
			{
				up = true;
				tookInput = true;
			}
			if(ke.getKeyCode() == 39)
			{
				right = true;
				tookInput = true;
			}
			if(ke.getKeyCode() == 40)
			{
				down = true;
				tookInput = true;
			}
		}
		if(ke.getID() == 402)
		{
			if(ke.getKeyCode() == 37)
			{
				left = false;
				tookInput = true;
			}
			if(ke.getKeyCode() == 38)
			{
				up = false;
				tookInput = true;
			}
			if(ke.getKeyCode() == 39)
			{
				right = false;
				tookInput = true;
			}
			if(ke.getKeyCode() == 40)
			{
				down = false;
				tookInput = true;
			}
		}
		if(tookInput)
			refactorBooleans();
		return tookInput;
	}
	
	public void refactorBooleans(){
		double newDirection = 0.0;
		int x = 0;
		int y = 0;
		if(up)
			y+=1;
		if(down)
			y-=1;
		if(left)
			x-=1;
		if(right)
			x+=1;
		if((x != 0) && (y != 0))
		{
			newDirection += Math.toDegrees(Math.atan((double)(y/x)));
			if(x == -1)
				newDirection+=180;
		}
		else if((x ==0) && (y != 0))
			newDirection = Math.toDegrees(Math.asin(y));
		else if((x != 0) && (y == 0))
			newDirection = Math.toDegrees(Math.acos(x));
		if(newDirection<0)
			newDirection = 360 - Math.abs(newDirection);
		if((x == 0) && (y == 0))
			setPath(new Path(getX(),getY()));
		else
			setPath(new LinearPath(getX(),getY(),newDirection));
	}
}
